package com.boc.langxiang.presenter.common;


import com.boc.langxiang.contract.common.LoginContract;
import com.boc.langxiang.entity.request.LoginRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class LoginPresenter implements LoginContract.Presenter {

    //第三方登陆名称（1是QQ  2是微信 3是微博）
    public static final String QQ = "1";
    public static final String WEIXIN = "2";
    public static final String SINA = "3";

    private final ApiService mService;
    private LoginContract.View mView;

    public LoginPresenter(LoginContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }

    @Override
    public void login(String phone, String password) {
        mView.showLoading();
        LoginRequest request = new LoginRequest();
        request.phone = phone;
        request.pwd = password;
        Observable<BaseResponse<UserInfo>> register = mService.login(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        UserInfo userInfo = response.getContent();
                        UserInfoManager.getInstance().setUserInfo(userInfo);
                        mView.loginSuccess();
                    }
                });
    }

//    @Override
//    public void authLogin(String lx, String openid) {
//        mView.showLoading();
//        AuthLoginRequest request = new AuthLoginRequest();
//        request.lx = lx;
//        request.openid = openid;
//        mService.authLogin(request.params())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
//                    @Override
//                    public void onGetPayInfoSuccess(BaseResponse<UserInfo> response) {
////                        UserInfo userInfo = response.getContent();
////                        UserInfoManager.getInstance().setUserInfo(userInfo);
////                        mView.loginSuccess();
//                    }
//                });
//    }
}
